package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.AccountOperateHistoryRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.PstAssign;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.AccountOperateHistory;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * 账号操作记录实现
 * Created by elvisxiong on 15/6/10.
 */
@Service
public class AccountOperateHistoryRdbServiceImpl extends DaoTemplate implements AccountOperateHistoryRdbService {

    @Override
    public List<AccountOperateHistory> findAll() {
        StringBuilder sql = new StringBuilder();
        sql.append("select * from account_operate_history order by date asc");
        return getJdbcTemplate().query(sql.toString(), DefaultRowMapper.autoRowMapper(AccountOperateHistory.class));
    }

    @Override
    public void save(final AccountOperateHistory accountOperateHistory) {
        final AccountOperateHistory accountOperateHistoryFinal = accountOperateHistory;
        String sql = "INSERT INTO account_operate_history (" +
                " date, operateType, accounts,userId "+
                ") VALUES (?,?,?,?)";
        PstAssign psta = new PstAssign() {
            @Override
            public void setParam(PreparedStatement ps) throws SQLException {
                ps.setLong(1, accountOperateHistoryFinal.getDate());
                ps.setInt(2, accountOperateHistoryFinal.getAccountOperateType().ordinal());
                ps.setString(3, accountOperateHistoryFinal.getAccounts());
                ps.setString(4, accountOperateHistoryFinal.getUserId());
            }
        };
        save(sql, psta);
    }

    @Override
    public List<AccountOperateHistory> findByUserId(final String userId) {
        StringBuilder sql = new StringBuilder();
        sql.append("select * from account_operate_history where userId = ? order by date desc");
        return getJdbcTemplate().query(sql.toString(), new Object[]{userId}, DefaultRowMapper.autoRowMapper(AccountOperateHistory.class));
    }
}
